<?php

namespace App\Kuafu\Statistics\Model;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cache;
use App\Kuafu\Base\Model\BaseModel;

class StatisticsModel extends BaseModel
{
    protected $connection = 'mysql';
    protected $table      = ''; // TODO 表名
    protected $primaryKey = 'id';
    protected $fillable   = []; // TODO 可更新字段，用来过滤create/save方法的参数

    protected $hidden = [
        'updatedAt',
        'isDeleted'
    ];

    public static function getDataPage(int $pageSize, int $pageNum)
    {
        return self::query()
                   ->orderByDesc('id')
                   ->paginate($pageSize, [], 'page', $pageNum);
    }

    public static function getDataById(int $id)
    {
        return self::query()->where('id', $id)->first();
    }

    public static function addData(array $data)
    {
        return self::query()->create($data);
    }

    public static function setData(int $id, array $data): int
    {
        $query = self::query()->where('id', $id)->first();
        if (!$query) {
            return 0;
        }

        foreach ($data as $k => $v) {
            $query->$k = $v;
        }

        return $query->save();
    }

    public static function setStatus(int $id, int $status): int
    {
        return self::query()->where('id', $id)->update(['status' => $status]);
    }

    public static function delData(int $id): int
    {
        return self::query()->where('id', $id)->update(['isDeleted' => 1]);
    }
}
